﻿using adminLibaryServer;
using adminLibaryServer.FACEDE;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace truongServer
{
    public class getConnectToServerCapCao
    {
        public static string url = null;
        static TcpChannel channel;
        private static totalFac _totalFace;
        public static totalFac TotalFace
        {
            get
            {
                if (channel == null)
                {
                    channel = new TcpChannel();
                    ChannelServices.RegisterChannel(channel, false);
                }
                if (_totalFace == null)
                {
                    try { _totalFace = (totalFac)RemotingServices.Connect(typeof(totalFac), url + "totalFac"); }
                    catch (Exception) { MessageBox.Show("Please restart Client !"); }
                }
                return _totalFace;
            }
        }

        private static adminFac _adminFac;
        public static adminFac AdminFac
        {
            get
            {
                if (channel == null)
                {
                    channel = new TcpChannel();
                    ChannelServices.RegisterChannel(channel, false);
                }
                if (_adminFac == null)
                {
                    try { _adminFac = (adminFac)RemotingServices.Connect(typeof(adminFac), url + "adminFac"); }
                    catch (Exception) { MessageBox.Show("Please restart Client !"); }
                }
                return _adminFac;
            }
        }

        private static RemotingObject _remotingObject;
        public static RemotingObject RemotingObject
        {
            get
            {
                if (channel == null)
                {
                    channel = new TcpChannel();
                    ChannelServices.RegisterChannel(channel, false);
                }
                if (_remotingObject == null)
                {
                    try { _remotingObject = (RemotingObject)RemotingServices.Connect(typeof(RemotingObject), url + "RemotingObject"); }
                    catch (Exception) { MessageBox.Show("Please restart Client !"); }
                }
                return _remotingObject;
            }
        }

    }

    public class updateAdmin
    {
        public int idTruong = 0;
        public string username = "";
        public string password = "";
        public Dictionary<string, string> readXML()
        {
            Dictionary<string,string> dic = new Dictionary<string, string>() ;
            try
            {
                if (File.Exists("server.xml"))
                {
                    XmlReader reader = XmlReader.Create("server.xml");
                    while (reader.Read())
                    {
                        if (reader.NodeType == XmlNodeType.Element && reader.Name == "server")
                        {
                            dic.Add(reader.GetAttribute(0), reader.GetAttribute(1));
                        }
                    }
                    reader.Close();
                    if (dic == null) {
                        writeXMLDefault();
                        dic = readXML();
                    }
                }
                else
                {
                    writeXMLDefault();
                    dic = readXML();
                }
            }
            catch (XmlException) {
                writeXMLDefault();
                dic = readXML();
            }
            return dic;
        }
        public void writeXMLDefault()
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            XmlWriter writer = XmlWriter.Create("server.xml", settings);
            writer.WriteStartDocument();
            writer.WriteStartElement("server");
            writer.WriteAttributeString("port", "9999");
            writer.WriteAttributeString("url", "localhost");
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Flush();
            writer.Close();
        }
        public  void writeXML(Hashtable myHashtable)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            XmlWriter writer = XmlWriter.Create("server.xml", settings);
            writer.WriteStartDocument();
            writer.WriteStartElement("server");
            writer.WriteAttributeString("port", myHashtable["port"].ToString());
            writer.WriteAttributeString("url", myHashtable["url"].ToString());
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Flush();
            writer.Close();
        }
        static string urlTCP;
        public void getURL()
        {
            Dictionary<string, string> dics = readXML();
            foreach (var dic in dics)
            {
                urlTCP = "tcp://" + dic.Value + ":" + dic.Key + "/";
                break;
            }
        }
        static int n = 0;
        public  int checkketNoi()
        {
            n++;
            try
            {
                getConnectToServerCapCao.url = urlTCP;
                adminFac admin = getConnectToServerCapCao.AdminFac;
                if (admin.adminInfor("parentID", "0") != null) return 1;
                else return 0;
            }
            catch (Exception) { return 0; }

        }
        public int getConnet()
        {
            int dt = checkketNoi();
            if (dt > 0) return 0;
            else
            {
                if (n < 4) return getConnet();
                else return 1;
            }
        }
    }
}
